<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form ref="queryParams" :model="queryParams" :inline="true" label-width="80px">
        <!--用户名-->
        <el-form-item label="登录账号" prop="account">
          <el-input v-model="queryParams.account" placeholder="请输入登录账号" style="width: 240px" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <!--手机号-->
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="queryParams.userName" placeholder="请输入用户名" style="width: 240px" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <!--用户状态-->
        <el-form-item label="用户状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择用户状态" size="small">
            <el-option label="全部" value="" />
            <el-option label="正常" value="1" />
            <el-option label="冻结" value="0" />
          </el-select>
        </el-form-item>
        <!--查询按钮-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!--表格数据-->
    <el-card>
      <div class="btn-box">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="btn_disabled" @click="handleDelete">批量删除</el-button>
      </div>
      <el-table v-loading="loading" :header-cell-style="{ background:'#f8f8f9'}" :data="userList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户编号" align="center" prop="userId" />
        <el-table-column label="登录账号" align="center" prop="account" />
        <el-table-column label="用户名" align="center" prop="username" :show-overflow-tooltip="true" />
        <el-table-column label="电子邮件" align="center" prop="email" :show-overflow-tooltip="true" />
        <el-table-column label="状态" align="center" prop="status">
          <template v-slot:default="scope">
            <el-tag :type="scope.row.status | statusFilter">{{ showDictValue(scope.row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页插件-->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!--添加或修改对话框-->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="datafrom" :model="datafrom" :rules="rules" label-width="80px">
        <el-row style="width: 540px">
          <el-col :span="24">
            <el-form-item label="登录账号" prop="account">
              <el-input v-model="datafrom.account" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="用户名称" prop="username">
              <el-input v-model="datafrom.username" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="电子邮件" prop="email">
              <el-input v-model="datafrom.email" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="用户状态" prop="status">
              <el-radio-group v-model="datafrom.status">
                <el-radio label="1" border size="small">正常</el-radio>
                <el-radio label="0" border size="small">冻结</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancal">取 消</el-button>
        <el-button :loading="btn_loading" type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getUser, userList, updateUserById, deleteUserById, saveUser } from '@/api/system'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      queryParams: {
        userName: '',
        account: '',
        status: '',
        pageNum: 1,
        pageSize: 10
      },
      // 没有任何选中行数时，禁用批量删除按钮
      btn_disabled: true,
      // 多相框选中数组
      ids: [],
      // 修改和添加对话框
      open: false,
      // 对话框确定按钮状态
      btn_loading: false,
      title: '',
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 参数表格数据
      userList: [],
      // 表单参数
      datafrom: {
        status: '1'
      },
      rules: {
        account: [
          { required: true, message: '登录账号不能为空', trigger: 'blur' },
          { min: 5, max: 12, message: '登录账号必须在5~12个字符', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '用户名称不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '电子邮件不能为空', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/, message: '电子邮件格式必须正确', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择用户状态', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      userList(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        // 查询成功 取消遮罩
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleUpdate(row) {
      this.reset()
      const userId = row.userId
      getUser(userId).then(response => {
        this.datafrom = response
        this.open = true
        this.title = '修改用户信息'
      })
    },
    handleDelete(row) {
      const userIds = row.userId || this.ids
      this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(() => {
        deleteUserById(userIds).then(response => {
          this.getList()
          this.$message.success(response)
        }).catch(() => {})
      }).catch(() => {})
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加用户信息'
    },
    cancal() {
      this.open = false
      this.reset()
    },
    submitForm: function() {
      this.$refs['datafrom'].validate((valid) => {
        if (valid) {
          this.btn_loading = true
          if (this.datafrom.userId !== undefined) {
            updateUserById(this.datafrom).then(response => {
              this.btn_loading = false
              this.$modal.msgSuccess('用户信息修改成功！')
              this.open = false
              this.reset()
              this.getList()
            })
          } else {
            saveUser(this.datafrom).then(response => {
              this.btn_loading = false
              this.$modal.msgSuccess('用户新信息新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    reset() {
      this.datafrom = {
        userId: undefined,
        account: undefined,
        username: undefined,
        email: undefined,
        status: '1'
      }
      this.resetForm('datafrom')
    },
    resetQuery() {
      this.resetForm('queryParams')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.btn_disabled = !selection.length
    },
    showDictValue(val) {
      let statusView
      switch (val) {
        case '1':
          statusView = '正常'
          break
        case '0':
          statusView = '冻结'
          break
        default:
          statusView = '其他'
          break
      }
      return statusView
    }
  }

}
</script>

<style scoped>
  .el-form >.el-form-item{
    margin-bottom: 3px;
    margin-top: 3px;
  }
  .box-card {
    margin-bottom: 10px;
  }
  .btn-box{
    margin-bottom: 10px;
  }
</style>
